package com.blt.car.home;

import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.home.adapter.CarListAdapter;
import com.blt.car.utils.Uiutils;

import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.car.CarApi;
import blt.app_core.api.car.bean.Car;
import blt.app_core.utils.CommonPref;
import blt.app_core.utils.PreferencesConstant;

/**
 * Created by xgp on 2017/6/19.
 */

public class CarListActivity extends BaseTitleBarActivity {
    public static final String CAR_DETAIL = "car_detail";

    SwipeMenuListView listView;
    private CarListAdapter carListAdapter;
    private int page = 1;
    private int pageSize = 30;
    private SwipeMenuItem openItem;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_car_list;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(CarApi.class).getCarList(page, pageSize);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("车辆列表");
        holder.get(R.id.carlist_add_car);
        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(AddCarActivity.class);
            }
        }, R.id.carlist_add_car);


        listView = holder.get(R.id.listView);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem modify = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                modify.setBackground(R.drawable.orange_background);
                // set item width
                modify.setWidth(Uiutils.dip2px(80));
                // set item title
                modify.setTitle("编辑");
                // set item title fontsize
                modify.setTitleSize(15);
                // set item title font color
                modify.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(modify);

                openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(R.drawable.blue_background
                );
                // set item width
                openItem.setWidth(Uiutils.dip2px(80));
                // set item title
                openItem.setTitle("选择");
                // set item title fontsize
                openItem.setTitleSize(15);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);


                SwipeMenuItem delete = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                delete.setBackground(R.drawable.red_background);
                // set item width
                delete.setWidth(Uiutils.dip2px(80));
                // set item title
                delete.setTitle("删除");
                // set item title fontsize
                delete.setTitleSize(15);
                // set item title font color
                delete.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(delete);

            }
        };

// set creator
        listView.setMenuCreator(creator);

        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                Car car = carListAdapter.getItem(position);

                switch (index) {
                    case 0:
                        if (car.carStatus == 2) {
                            openItem.setBackground(R.drawable.gray_background);
                            toastShort("未通过的车辆不能编辑");
                            return false;
                        }

                        openActivity(AddCarActivity.class, AddCarActivity.CAR_INFO, car);
                        break;
                    case 1:
                        // chose
                        if (car.carStatus == 0 || car.carStatus == 2) {
                            openItem.setBackground(R.drawable.gray_background);
                            toastShort("待审核的车辆不能选择");
                            return false;
                        }
                        CoreManager.notifyClients(CarApi.class, CarApi.onUpdataHomeCar, car);
                        CoreManager.getCore(CarApi.class).setDefaultCar(car.carId);
                        CommonPref.getInstance().saveInt(PreferencesConstant.CAR_ID, car.carId);
                        finish();
                        break;
                    case 2:
                        // delete
                        int defaultId = CommonPref.getInstance().getInt(PreferencesConstant.CAR_ID, 0);
                        if (defaultId == car.carId) {
                            toastShort("被选择的车辆不能删除");
                            return false;
                        }
                        carListAdapter.removeCar(position);
                        break;

                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });

        // Right
        listView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);


        carListAdapter = new CarListAdapter(this);
        listView.setAdapter(carListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Car car = carListAdapter.getCarList().get(position);
                openActivity(CarDetailActivity.class, CAR_DETAIL, car);
            }
        });

    }

    @CoreEvent(coreClientClass = CarApi.class)
    public void onCarList(List<Car> data) {
        carListAdapter.addData(data);
    }

    @CoreEvent(coreClientClass = CarApi.class)
    public void updateList() {
        CoreManager.getCore(CarApi.class).getCarList(page, pageSize);
    }
}
